<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业实现低成本数字化转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\payment;

use app\common\dao\BaseDao;
use app\common\model\payment\PaymentLogModel;

class PaymentLogDao extends BaseDao
{

    /**
     * @return PaymentLogModel
     */
    protected function getModel(): string
    {
        return PaymentLogModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
                $query->where('status', $where['status']);
            })
            ->when(isset($where['use_type']) && $where['use_type'] !== '', function ($query) use ($where) {
                $query->where('use_type', $where['use_type']);
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('order_sn|remark', '%' . $where['keywords'] . '%');
            })
            ->when(isset($where['time']) && $where['time'] !== '', function ($query) use ($where) {
                $this->timeSearchBuild($query, $where['time'], 'add_time');
            });
    }

    /**
     * 添加支付日志
     * @param string $clientType 客户端类型
     * @param string $payType 支付类型
     * @param int $useType 操作类型
     * @param int $userId 会员id
     * @param float $amount 支付金额
     * @param string $remark 备注
     * @param array $params 其他参数
     * @param array $payeeInfo 收款方信息
     * @return BaseDao|\think\Model
     */
    public function addLog($propertyId, $clientType, $payType, $useType, $userId, $amount, $remark, $params = [], $payeeInfo = [])
    {
        $data = [
            'property_id' => $propertyId,
            'client_type' => $clientType,
            'user_id' => $userId,
            'amount' => $amount,
            'pay_type' => $payType,
            'use_type' => $useType,
            'remark' => $remark,
            'params' => json_encode($params),
            'order_sn' => $this->generateOrderSn()
        ];
        if (isset($payeeInfo['property_id'])) {
            $data['property_id'] = $payeeInfo['property_id'];
        }
        return $this->create($data);
    }

    /**
     *  生成订单编号
     * @return string
     * @throws \think\db\exception\DbException
     *
     * @author Amos
     * 2024-06-15 11:30
     */
    public function generateOrderSn()
    {
        while (true) {
            $number = 'SN' . date('ymd') . rand(111, 999) . date('His') . rand(111, 999);
            $count = $this->getSearch([])->where('order_sn', $number)->count();
            if ($count == 0) {
                break;
            }
        }

        return $number;
    }

    /**
     * 根据订单号查询信息
     * @param $orderSn
     * @return array|mixed|\think\db\BaseQuery|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-15 11:30
     */
    public function getByOrderSn($orderSn)
    {
        return $this->getModel()::getDB()->where('order_sn', $orderSn)->find();
    }
}